// -------------------------------------------------
// BlinKit - BlinKit Library
// -------------------------------------------------
//   File Name: web_page_host.cpp
// Description: WebPageHost Class
//      Author: Ziming Li
//     Created: 2022-05-02
// -------------------------------------------------
// Copyright (C) 2022 MingYang Software Technology.
// -------------------------------------------------

#include "./web_page_host.h"

#include "blinkit/ui/compositor/layer_tree_host.h"
#include "blinkit/ui/compositor/tasks/raster_task.h"
#include "blinkit/ui/web_page.h"

namespace BlinKit {

WebPageHost::WebPageHost(WebPage *page)
    : m_page(page)
    , m_layerTreeHost(std::make_unique<LayerTreeHost>(*this))
{
}

WebPageHost::~WebPageHost(void) = default;

bool WebPageHost::CanBeClosed(void) const
{
    ASSERT_NOT_REACHED();
    return true;
}

WebLayerTreeView* WebPageHost::GetLayerTreeView(void) const
{
    return m_layerTreeHost.get();
}

WebScreenInfo WebPageHost::GetScreenInfo(void)
{
    ASSERT_NOT_REACHED();
    return WebScreenInfo();
}

void WebPageHost::Rasterize(void)
{
    const IntSize &size = m_page->Size();
    if (size.isEmpty())
        return;

    std::unique_ptr<RasterTask> task = std::make_unique<RasterTask>(size, *this);
    m_layerTreeHost->Commit(task);
}

void WebPageHost::Resize(const IntSize &size)
{
    m_page->Resize(size);
    m_layerTreeHost->ViewportSizeUpdated();
}

void WebPageHost::SetToolTipText(const String &tooltipText, TextDirection dir, unsigned long cookie)
{
    if (tooltipText.isEmpty())
        HideTooltip();
    else
        ShowToolTip(tooltipText.stdUtf8(), dir, cookie);
}

} // namespace BlinKit
